package com.liukai.novel.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.view.WindowManager;

import androidx.core.app.ActivityCompat;

import com.liukai.novel.R;
import com.liukai.novel_base.base.BaseActivity;
import com.liukai.novel_commons.Result;
import com.liukai.novel_commons.db.UserInfoService;
import com.liukai.novel_commons.entities.NovelRack;
import com.liukai.novel_commons.entities.UserInfo;
import com.liukai.novel_commons.utils.Constans;
import com.liukai.novel_commons.utils.JSONUtil;
import com.liukai.novel_commons.utils.LogUtils;
import com.liukai.novel_commons.utils.MyInfoList;
import com.liukai.novel_rack.call_back.IRackCallBack;
import com.liukai.novel_rack.presenter.impl.RackPresenterImpl;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;

public class SplashActivity extends BaseActivity implements IRackCallBack {

    @BindView(R.id.avi)
    AVLoadingIndicatorView mIndicatorView;

    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.WRITE_EXTERNAL_STORAGE" };


    /**
     * 检查并且获取权限
     * @param activity
     */
    public static void verifyStoragePermissions(Activity activity) {

        try {
            //检测是否有写的权限
            int permission = ActivityCompat.checkSelfPermission(activity,
                    "android.permission.WRITE_EXTERNAL_STORAGE");
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // 没有写的权限，去申请写的权限，会弹出对话框
                ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected int getLayoutId() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return R.layout.activity_splash;
    }

    @Override
    protected void initData(Context context) {
        verifyStoragePermissions(this);
        // 在启动页面时判断用户是否登录，如果登录，则从后台加载书架信息；未登录，从本地加载书架信息
        LogUtils.e(getClass(),"context==========>" + context.toString());
        UserInfoService userInfoService = new UserInfoService(context);
        List<UserInfo> userInfoList = userInfoService.findUserInfo();
        if (userInfoList.size() <= 0){
            loading();
            return;
        }
        UserInfo userInfo = userInfoList.get(0);
        Constans.setUserInfo(userInfo);
        MyInfoList.addSettingList(Constans.LOGOUT);
        RackPresenterImpl rackPresenter = new RackPresenterImpl();
        rackPresenter.registerCallback(this);
        rackPresenter.findAllRackByUserId(userInfo.getUserId());
    }

    @Override
    protected void initView() {
        mIndicatorView.show();
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void release() {
        super.release();
        mIndicatorView.hide();
    }

    /**
     * 返回用户的书架信息
     *
     * @param result
     */
    @Override
    public void findAllRackByUserIdResult(Result result) {
        if (result.getCode() != 200)
            return;
        // 解析后台传回来的书架信息
        List<NovelRack> novelRackList = JSONUtil.resolverResultList(result, NovelRack.class);
        Constans.setNovelRackList(novelRackList);
        loading();
    }

    @Override
    public void loadMuLuResult(Result result) {

    }

    @Override
    public void deleteRackById(Result result) {

    }

    private void loading(){
        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
                startActivity(new Intent(getApplicationContext(),MainActivity.class));
                finish();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

}
